package org.yookiee.support.core;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import lombok.Builder;
import lombok.Data;
import org.yookiee.support.enums.Scope;
import org.yookiee.support.exceptions.NotSupportKeyException;
import org.yookiee.support.exceptions.NotSupportValueException;
import org.yookiee.support.toolkits.MapToolkit;

import java.util.Map;

/**
 * FileName KeyExpression
 * Package org.yookiee.support.core
 * Description
 * author cpstory
 * create 2023-12-01 13:17
 * version V1.0
 */
@Data
@Builder(toBuilder = true)
public class KeyExpression {

    private static final String LOCAL_SUPPORT_PREFIX = "easy-i18n";
    private static final String LOCAL_SUPPORT_SEPARATOR = "\\.";
    private static final int LOCAL_SUPPORT_ELEMENT_COUNT = 4;
    private static final int LOCAL_SUPPORT_PREFIX_INDEX = 0;
    private static final int LOCAL_SUPPORT_SCOPE_INDEX = 1;
    private static final int LOCAL_SUPPORT_MODULE_INDEX = 2;
    private static final int LOCAL_SUPPORT_CODE_INDEX = 3;

    private String scope;
    /**模块*/
    private String modules;
    /**标签*/
    private String labels;

    /**
     * 将key解析为表达式
     * @param key
     * @return
     */
    public static KeyExpression format(String key){
        if(Strings.isNullOrEmpty(key)){
            return null;
        }
        if(!key.trim().startsWith(LOCAL_SUPPORT_PREFIX)){
            throw new NotSupportKeyException(String.format("please check key in your properties file, " +
                    "the key must be start with easy-i18n, your error key is: %s",key));
        }
        //检查key中的分隔符是否符合要求

        String[] split = key.split(LOCAL_SUPPORT_SEPARATOR);
        if(split.length != LOCAL_SUPPORT_ELEMENT_COUNT){
            throw new NotSupportKeyException(String.format("please check key in your properties file, " +
                    "we have not supported '.' definition by yourself, your error key is: %s",key));
        }

        Scope scope = Scope.format(split[LOCAL_SUPPORT_SCOPE_INDEX].trim());
        if(scope == null) {
            throw new NotSupportKeyException(String.format("please check key in your properties file, it needs to be composed of " +
                    "easy-i18n.scope(java/web).module(definition by yourself).code(definition by yourself like #code), your error key is: %s",key));
        }

        return KeyExpression.builder()
                .scope(scope.getScope())
                .modules(split[LOCAL_SUPPORT_MODULE_INDEX].trim())
                .labels(split[LOCAL_SUPPORT_CODE_INDEX].trim())
                .build();
    }

    public static SysI18nAllocate adapt(String key,String langs){
        KeyExpression expression = format(key);
        if(expression == null){
            return null;
        }
        SysI18nAllocate allocate = SysI18nAllocate.builder()
                .scope(expression.getScope().trim())
                .modules(expression.getModules().trim())
                .labels(expression.getLabels().trim())
                .langs(langs.trim()).build();

        //转换出langMeta
        if(!Strings.isNullOrEmpty(langs)){
            try{
                JSONObject jsonObject = JSON.parseObject(langs);
                Map<String, String> metaMap = MapToolkit.jsonObjectToMap(jsonObject);
                allocate.setLangMeta(metaMap);
            }catch (Exception e){
                throw new NotSupportValueException("not support language config, please check your properties file, " +
                        "please ensure it is in JSON format");
            }

        }
        return allocate;
    }


}
